import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Table} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {
    PAGE,
    TABLE,
    FORM_BIG_OPTIONS,
    PAGINATION_OPTIONS,
    COMMON_TABLE_QUERY_FAIL_TEXT,
    COMMON_TABLE_EMPTY_TEXT,
} from '../constants';
import routes from './routes';
import {Link} from 'react-router-dom';
import {conventSorter, getObjById} from './utils';
const ButtonGroup = Button.Group;
const FormItem = Form.Item;
import WrappedTag from '../common/WrappedTag';
import styles from './style.css';

class QueryPanel extends React.PureComponent {
    state = {
        loading: false,
    };

    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({
                ...conventSorter(sorter),
                pageIndex: PAGE.index
            });
    };

    render() {
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'workItemHighPanel.column.code',
                    defaultMessage: '工位编号'
                }),
                dataIndex: 'code',
                sorter: true,
                render: (text, record) => <Link to={routes.detail.format(record.id)} >{text}</Link>
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'workItemHighPanel.column.name',
                    defaultMessage: '工位名称'
                }),
                dataIndex: 'name',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'workItemHighPanel.column.productCategoryCode',
                    defaultMessage: '车型编号'
                }),
                sorter: true,
                dataIndex: 'productCategoryCode'
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'workItemHighPanel.column.laborHour',
                    defaultMessage: '工时'
                }),
                sorter: true,
                dataIndex: 'laborHour'
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'workItemHighPanel.column.partCode',
                    defaultMessage: '备件编号'
                }),
                sorter: true,
                dataIndex: 'partCode'
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'workItemHighPanel.column.partName',
                    defaultMessage: '备件名称'
                }),
                dataIndex: 'partName'
            }
        ];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const {productCategoryId, productCategoryCode, productCategoryName} = this.props.conditions;
        const productCategoryInfo = productCategoryId ? {
            id: productCategoryId,
            code: productCategoryCode,
            name: productCategoryName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_BIG_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'workItemHighPanel.form.code',
                                    defaultMessage: '编号'
                                })}
                                {...FORM_BIG_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_BIG_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'workItemHighPanel.form.name',
                                    defaultMessage: '名称'
                                })}
                                {...FORM_BIG_OPTIONS.item}>
                                <TextInput
                                    name="name"
                                    value={this.props.conditions.name}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_BIG_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'workItemHighPanel.form.partCode',
                                    defaultMessage: '备件编号'
                                })}
                                {...FORM_BIG_OPTIONS.item}>
                                <TextInput
                                    name="partCode"
                                    value={this.props.conditions.partCode}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                        {this.props.categoryName
                            ? <Col {...FORM_BIG_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'workItemPanel.form.category',
                                        defaultMessage: '分类'
                                    })}
                                    {...FORM_BIG_OPTIONS.item}>
                                    <WrappedTag className={styles.tagFontSize} color="blue"
                                        closable onClose={this.props.onCloseTag}>{this.props.categoryName}</WrappedTag>
                                </FormItem>
                            </Col> : null}
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={16}>
                            <ButtonGroup>
                                <Button
                                    key="query"
                                    type="primary"
                                    onClick={this.props.querySearch}
                                    loading={this.props.loading}>
                                    {this.props.intl.formatMessage({
                                        id: 'workItemHighPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}
                                </Button>
                            </ButtonGroup>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'workItemHighPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                    </Row>

                    <Table
                        className="white-space-nowrap"
                        columns={columns}
                        dataSource={this.props.data}
                        rowKey="id"
                        pagination={pagination}
                        loading={this.props.loading || this.state.loading}
                        onChange={this.onChangeSort}
                        locale={locale}
                        {...TABLE} />
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object,
    intl: PropTypes.object,
    loading: PropTypes.bool,
    permissions: PropTypes.array,
    querySearch: PropTypes.func,
    onClickReset: PropTypes.func,
    onConditionsChange: PropTypes.func,
    onConditionsPressEnter: PropTypes.func,
    onSelect: PropTypes.func
};
import {connect} from 'react-redux';
import {
    saveHighQueryCondition as saveQueryCondition,
    queryHighSearch as querySearch,
    resetHighQueryPanel as resetQueryPanel,
    tableHighSearch as tableSearch,
    categoryHighSearch as categorySearch,
    selectPC
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryHighCondition']);

const getData = selectorFactory(['page', 'domainData', 'workItemProductCategories', 'data']);

const getCategoryName = createSelector(
    state => state.getIn(['page', 'domainData', 'categories', 'data']),
    state => state.getIn(['page', 'appState', 'queryHighCondition', 'workItemCategoryId']),
    (categories, categoryId) => {
        if(!categoryId)
            return '';
        const category = getObjById(categories.toJS(), categoryId);
        return category ? category.name : '';
    }
);

const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'workItemProductCategories', 'isFetching']),
    permissions: getPermissions(state),
    data: getData(state),
    errorMessage: state.getIn(['page', 'domainData', 'workItemProductCategories', 'message']),
    total: state.getIn(['page', 'domainData', 'workItemProductCategories', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryHighBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryHighBySort', 'pageSize']),
    categoryName: getCategoryName(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (value, name) => dispatch(saveQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveQueryCondition(name, value));
        dispatch(querySearch());
    },
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onPageSizeChange: (pageIndex, pageSize) =>
        dispatch(
            tableSearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableSearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableSearch(option)),
    onCloseTag: () => dispatch(categorySearch()),
    onSelect: data => dispatch(selectPC(data)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
